package dao.impl;

import dao.CommodityClassDao;
import pojo.Commodity;
import pojo.CommodityClass;
import util.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类接口的实现类
 * @author 叶军军
 */
public class CommodityClassDaoImpl implements CommodityClassDao {

    /**
     *查询所有的商品分类
     */
    @Override
    public List<CommodityClass> queryCommodityClass() {
        List<CommodityClass> list=new ArrayList<CommodityClass>();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn= BaseDao.getConnection();
            String sql="select * from goodsclass";
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()){
                CommodityClass commodityClass=new CommodityClass();
                commodityClass.setClId(rs.getInt("cl_id"));
                commodityClass.setClName(rs.getString("cl_name"));
                commodityClass.setClDescribe(rs.getString("cl_describe"));
                commodityClass.setFaId(rs.getInt("fa_id"));
                list.add(commodityClass);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.release(rs,ps,conn);
        }
        return list;
    }

    /**
     * 根据搜索关键字查询商品分类
     */
    @Override
    public List<CommodityClass> queryCommodityClassByKey(String value) {
        List<CommodityClass> list=new ArrayList<CommodityClass>();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn= BaseDao.getConnection();
            String sql="select * from goodsclass where fa_id=100 " +
                    "and (cl_name like '%"+value+"%' or cl_describe like '%"+value+"%')";
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()){
                CommodityClass commodityClass=new CommodityClass();
                commodityClass.setClId(rs.getInt("cl_id"));
                commodityClass.setClName(rs.getString("cl_name"));
                commodityClass.setClDescribe(rs.getString("cl_describe"));
                commodityClass.setFaId(rs.getInt("fa_id"));
                list.add(commodityClass);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.release(rs,ps,conn);
        }
        return list;
    }

    /**
     *根据父类Id查询商品集合
     */
    @Override
    public List<Commodity> queryCommodityByClId(int id) {
        List<Commodity> list=new ArrayList<Commodity>();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn= BaseDao.getConnection();
            String sql="select g.* from goods g,goodsclass gc " +
                    "where g.cl_id=gc.cl_id and gc.fa_id=?";
            ps=conn.prepareStatement(sql);
            ps.setInt(1,id);
            rs=ps.executeQuery();
            while (rs.next()){
                Commodity commodity=new Commodity();
                commodity.setGoId(rs.getInt("go_id"));
                commodity.setGoName(rs.getString("go_name"));
                commodity.setGoSalesvolume(rs.getInt("go_salesvolume"));
                commodity.setGoPraise(rs.getInt("go_praise"));
                commodity.setGoUpTime(rs.getTimestamp("go_uptime"));
                commodity.setGoPictureUrl(rs.getString("go_pictureurl"));
                commodity.setGoDescribe(rs.getString("go_describe"));
                commodity.setGoCost(rs.getInt("go_cost"));
                commodity.setGoPrice(rs.getInt("go_price"));
                commodity.setGoStock(rs.getInt("go_stock"));
                commodity.setGoState(rs.getInt("go_state"));
                commodity.setClId(rs.getInt("cl_id"));
                commodity.setGoSeckill(rs.getInt("go_seckill"));
                list.add(commodity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.release(rs,ps,conn);
        }
        return list;
    }
}
